package com.chazz.service.impl;

import cn.hutool.db.Db;
import cn.hutool.db.Entity;
import com.chazz.dao.impl.CouponDaoImpl;
import com.chazz.dao.impl.DiscountDaoImpl;
import com.chazz.dao.impl.PriceDaoImpl;
import com.chazz.service.CoupondService;
import java.sql.SQLException;
import java.text.DecimalFormat;

/**
 * 描述:根据商品id判断是否使用优惠券
 *
 * @author Chazz
 * @create 2020-09-24 14:53
 */
public class CoupondServiceImpl implements CoupondService {

    /**
     * description: IsCoupond  根据商品id判断是否使用优惠券,并返回计算优惠券之后的价格<br>
     * version: 1.0 <br>
     * date: 2020/9/24 14:54 <br>
     * author: Chazz <br>
     * @param goodsId 商品id
     * @param uid 用户id
     * @param count 商品数量
     * @return double 商品价格小计
     */
    @Override
    public String isCoupond(Integer goodsId, Integer uid, Integer count) {
        Integer categoryId = new PriceDaoImpl().selectCategoryId(goodsId);
        double discount = new DiscountDaoImpl().selectDiscountById(categoryId);
        double price = new PriceDaoImpl().selectPrice(goodsId);
        Double totalPrice = 0.0;
        if (discount==1) {
            Integer coupond = new CouponDaoImpl().isCoupond(uid, goodsId);
            totalPrice = price - coupond +price*(count-1);
        } else {
            totalPrice = count*price;
        }
        //String totalPriceStr = totalPrice.toString();
        DecimalFormat df = new DecimalFormat("#.00");
        String totalPriceStr = df.format(totalPrice);
        int i=0;
        try {
            i = Db.use().insert(Entity.create("orders").set("tatolprice", totalPriceStr));
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return totalPriceStr;
    }
}
